package com.haochang.chunkvod.tools.vod.goods;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.haochang.base.EventProxy;
import com.haochang.base.IEventConstant;
import com.haochang.chunkvod.app.common.VodConstant;
import com.haochang.chunkvod.app.database.order.GoodsInfoDao;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.TimeUtils;
import com.haochang.chunkvod.model.order.ActiveInfo;
import com.haochang.chunkvod.model.order.FoodDetailInfo;
import com.haochang.chunkvod.model.order.FoodInfo;
import com.haochang.chunkvod.model.order.GoodsInfo;
import com.haochang.chunkvod.model.order.WeChatInfo;

import android.text.TextUtils;

/**
 * @author qintao
 *         created at 2016/6/21 18:09
 *         购物车管理类
 *         商品类型：0默认为单品，1套餐，2活动
 */

public class ShoppingCartManager
{

	/**
	 * 添加套餐详情到购物车
	 *
	 * @param goodInfo 套餐信息
	 * @param foodInfoList 套餐商品详情信息
	 * @param isInsertDb 是否插入数据库
	 */
	public static GoodsInfo addPackageDetailToGoods(GoodsInfo goodInfo, List<FoodInfo> foodInfoList, boolean isInsertDb)
	{
		if (goodInfo == null || CollectionUtils.isEmpty(foodInfoList))
			return null;
		GoodsInfo info = new GoodsInfo();
		info.setIs_present(goodInfo.is_present());//2016/12/03 设置是否赠送
		info.setGoodsId(goodInfo.getGoodsId());
		info.setRoomId(goodInfo.getRoomId());
		info.setBillId(goodInfo.getBillId());
		info.setMenuId(goodInfo.getMenuId());
		info.setGoodsName(goodInfo.getGoodsName());
		info.setIs_give(goodInfo.is_give());
		info.setOrderNum(goodInfo.getOrderNum() <= 0 ? 1 : goodInfo.getOrderNum());
		info.setPrice(goodInfo.getPrice());
		info.setUnit(goodInfo.getUnit());
		info.setGoods_type(VodConstant.BUNDLE_TYPE);//套餐类型为1，固定
		JSONArray array = new JSONArray();
		for (int i = 0; i < foodInfoList.size(); i++)
		{
			try
			{
				FoodInfo foodInfo = foodInfoList.get(i);
				List<FoodDetailInfo> detailInfoList = foodInfo.getFoodList();
				if (!CollectionUtils.isEmpty(detailInfoList))
				{
					if (foodInfo.getType().equals("master"))
					{
						for (int j = 0; j < detailInfoList.size(); j++)
						{
							FoodDetailInfo detailInfo = detailInfoList.get(j);
							if (detailInfo.getQuantity() > 0)
							{
								JSONObject obj = new JSONObject();
								obj.put("name", detailInfo.getName());
								obj.put("number", detailInfo.getQuantity());
								obj.put("unit", detailInfo.getUnit());
								obj.put("flavor_array", detailInfo.getFlavor());
								obj.put("standard", detailInfo.getStandard());
								obj.put("standard_type", detailInfo.getStandard_types());
								obj.put("combo_id", detailInfo.getCombo_id());
								obj.put("taste", detailInfo.getTaste_types());
								obj.put("goods_id", detailInfo.getFood_id());
								obj.put("combos", "");
								array.put(obj);
							}
						}
					} else
					{
						for (int j = 0; j < detailInfoList.size(); j++)
						{
							FoodDetailInfo detailInfo = detailInfoList.get(j);
							if (detailInfo.getNumber() > 0)
							{
								JSONObject obj = new JSONObject();
								obj.put("name", detailInfo.getName());
								obj.put("number", detailInfo.getNumber());
								obj.put("unit", detailInfo.getUnit());
								obj.put("taste", detailInfo.getTaste_types());
								obj.put("flavor_array", detailInfo.getFlavor());
								obj.put("standard", detailInfo.getStandard());
								obj.put("standard_type", detailInfo.getStandard_types());
								obj.put("combo_id", detailInfo.getCombo_id());
								obj.put("goods_id", detailInfo.getFood_id());
								obj.put("combos", "");
								array.put(obj);
							}
						}
					}
				}
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		info.setGoodsDetial(array.toString());
		info.setOrderTime(TimeUtils.getNowTime());
		if (isInsertDb)
		{
			GoodsInfoDao.getDao().insert(GoodsInfo.class, info);
			EventProxy.notifyEvent(IEventConstant.EVENT_SHOP_CART, info);
			EventProxy.notifyEvent(IEventConstant.ADD_SHOP_CART_SUCCESS, info);
		}
		return info;
	}

	/**
	 * 添加活动详情到购物车
	 *
	 * @param goodInfo 活动信息
	 * @param activeInfoList 活动商品详情信息
	 * @param isInsertDb 是否插入数据库   
	 */
	public static GoodsInfo addActiveDetailToGoods(GoodsInfo goodInfo, List<ActiveInfo> activeInfoList, boolean isInsertDb)
	{
		if (goodInfo == null)
			return null;
		GoodsInfo info = new GoodsInfo();
		info.setIs_present(goodInfo.is_present());//2016/12/03 设置是否赠送
		info.setGoodsId(goodInfo.getGoodsId());
		info.setRoomId(goodInfo.getRoomId());
		info.setBillId(goodInfo.getBillId());
		info.setMenuId(goodInfo.getMenuId());
		info.setGoodsName(goodInfo.getGoodsName());
		info.setIs_give(goodInfo.is_give());
		info.setOrderNum(1);//活动每次默认添加一个
		info.setPrice(goodInfo.getPrice());
		info.setUnit(goodInfo.getUnit());
		info.setGoods_type(VodConstant.ACTIVE_TYPE);//活动类型为2，固定
		info.setCharging_room_type_id(goodInfo.getCharging_room_type_id());
		JSONArray array = new JSONArray();
		if (goodInfo.getDecrease_time() > 0)
		{
			JSONObject obj = new JSONObject();
			try
			{
				obj.put("name", "减免" + goodInfo.getDecrease_time() + "分钟房费");
				obj.put("number", 0);
				obj.put("unit", "");
				obj.put("flavor_array", "");
				obj.put("standard", "");
				obj.put("goods_id", "");
				obj.put("combos", "");
				array.put(obj);
			} catch (JSONException e)
			{
				e.printStackTrace();
			}
		}
		if (!CollectionUtils.isEmpty(activeInfoList))
		{
			try
			{
				for (int i = 0; i < activeInfoList.size(); i++)
				{
					ActiveInfo activeInfo = activeInfoList.get(i);
					JSONObject obj = new JSONObject();
					obj.put("name", activeInfo.getCn_name());
					obj.put("unit", activeInfo.getUnit());
					obj.put("flavor_array", activeInfo.getFlavor());
					obj.put("standard", activeInfo.getStandard());
					obj.put("standard_type", activeInfo.getStandard_types());
					obj.put("goods_id", activeInfo.getGoods_id());
					obj.put("taste", activeInfo.getTaste_types());
					if (TextUtils.isEmpty(activeInfo.getCombos()) || activeInfo.getCombos().equals("[]"))
					{
						//活动里面的单品
						obj.put("number", activeInfo.getOrderNum());
						if (activeInfo.getOrderNum() > 0)
						{
							obj.put("combos", "");
							array.put(obj);
						}
					} else
					{
						//活动里面的套餐
						obj.put("number", 1);//活动里面的套餐每次默认点一份
						List<FoodInfo> foodInfoList = activeInfo.getFoodList();
						if (CollectionUtils.isEmpty(foodInfoList))
							continue;
						JSONArray jsonArray = new JSONArray();
						boolean is_value = false;
						for (int k = 0; k < foodInfoList.size(); k++)
						{
							FoodInfo foodInfo = foodInfoList.get(k);
							List<FoodDetailInfo> detailInfoList = foodInfo.getFoodList();
							if (CollectionUtils.isEmpty(detailInfoList))
								continue;
							if (foodInfo.getType().equals("master"))
							{
								for (int j = 0; j < detailInfoList.size(); j++)
								{
									FoodDetailInfo detailInfo = detailInfoList.get(j);
									if (detailInfo.getQuantity() > 0)
									{
										JSONObject object = new JSONObject();
										object.put("name", detailInfo.getName());
										object.put("number", detailInfo.getQuantity());
										object.put("unit", detailInfo.getUnit());
										object.put("taste", detailInfo.getTaste_types());
										object.put("flavor_array", detailInfo.getFlavor());
										object.put("standard", detailInfo.getStandard());
										object.put("standard_type", detailInfo.getStandard_types());
										object.put("combo_id", detailInfo.getCombo_id());
										object.put("goods_id", detailInfo.getFood_id());
										is_value = true;
										jsonArray.put(object);
									}
								}
							} else
							{
								for (int j = 0; j < detailInfoList.size(); j++)
								{
									FoodDetailInfo detailInfo = detailInfoList.get(j);
									if (detailInfo.getNumber() > 0)
									{
										JSONObject object = new JSONObject();
										object.put("name", detailInfo.getName());
										object.put("number", detailInfo.getNumber());
										object.put("unit", detailInfo.getUnit());
										object.put("taste", detailInfo.getTaste_types());
										object.put("flavor_array", detailInfo.getFlavor());
										object.put("standard", detailInfo.getStandard());
										object.put("standard_type", detailInfo.getStandard_types());
										object.put("combo_id", detailInfo.getCombo_id());
										object.put("goods_id", detailInfo.getFood_id());
										is_value = true;
										jsonArray.put(object);
									}
								}
							}
						}
						if (is_value)
						{
							obj.put("combos", jsonArray);
							array.put(obj);
						}
					}
				}
			} catch (JSONException e)
			{
				e.printStackTrace();
			}
		}
		if (array.length() > 0)
			info.setGoodsDetial(array.toString());
		info.setOrderTime(TimeUtils.getNowTime());
		if (isInsertDb)
		{
			GoodsInfoDao.getDao().insert(GoodsInfo.class, info);
			EventProxy.notifyEvent(IEventConstant.EVENT_SHOP_CART, info);
			EventProxy.notifyEvent(IEventConstant.ADD_SHOP_CART_SUCCESS, info);
		}
		return info;
	}

	/**
	 * 清空购物车
	 *
	 * @param bill_id 账单id
	 */
	public static void clearGoods(String bill_id)
	{
		GoodsInfoDao.getDao().deleteBuilder(GoodsInfo.class, "bill_id", bill_id);
		EventProxy.notifyEvent(IEventConstant.EVENT_ORDER_SHOP_CART, 0);
	}

	/**
	 * 用于每半小时检查一次购物车，不满足条件的就清空购物车
	 */
	public static void checkGoods()
	{
		List<GoodsInfo> list = GoodsInfoDao.getDao().queryMinTimeGoodInfos();
		for (GoodsInfo goodsInfo : list)
		{
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = goodsInfo.getOrderTime();
			if (!TextUtils.isEmpty(time))
			{
				try
				{
					Date dt2 = new Date();
					Date dt1 = df.parse(time);
					if (dt2.getTime() - dt1.getTime() > 60000)
					{
						GoodsInfoDao.getDao().deleteBuilder(GoodsInfo.class, "bill_id", goodsInfo.getBillId());
					}
				} catch (ParseException e)
				{
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 购物车删除某一商品
	 *
	 * @param goodsInfo 商品
	 */
	public static void deleteGoods(GoodsInfo goodsInfo)
	{
		GoodsInfoDao.getDao().delete(GoodsInfo.class, goodsInfo);
		int totalNum = GoodsInfoDao.getDao().queryGoodsInfoNum(goodsInfo.getBillId());
		EventProxy.notifyEvent(IEventConstant.EVENT_ORDER_SHOP_CART, totalNum);
	}

	/**
	 * 购物车删除活动
	 *
	 * @param bill_id 账单id
	 */
	public static void deleteActiveGoods(String bill_id)
	{
		GoodsInfoDao.getDao().deleteBuilder(GoodsInfo.class, "bill_id", bill_id, "goods_type", 2);
		int totalNum = GoodsInfoDao.getDao().queryGoodsInfoNum(bill_id);
		EventProxy.notifyEvent(IEventConstant.EVENT_ORDER_SHOP_CART, totalNum);
	}

	/**
	 * 通过商品类型返回下单时需要的数据
	 * @param goodInfo
	 * @return
	 */
	public static JSONArray getGoodsArrayByType(GoodsInfo goodInfo)
	{
		JSONArray jsonArray = null;
		try
		{
			if (goodInfo.getGoods_type() == VodConstant.RETAIL_TYPE)
			{
				jsonArray = getGoodsArray(goodInfo);
			} else if (goodInfo.getGoods_type() == VodConstant.BUNDLE_TYPE)
			{
				jsonArray = getPackageDataBeforeAddToShop(goodInfo, false);
			} else
			{
				jsonArray = getActiveDataBeforeAddToShop(goodInfo, false);
			}
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return jsonArray;
	}

	/**
	 * 获取套餐添加到购物车之前的数据
	 * @return
	 */
	public static JSONArray getPackageDataBeforeAddToShop(GoodsInfo goodsInfo, boolean isInsertDb)
	{
		JSONArray jsonArray = null;
		try
		{
			jsonArray = new JSONArray();
			JSONArray bundleArray = null;
			if (TextUtils.isEmpty(goodsInfo.getGoodsDetial()))
				bundleArray = new JSONArray();
			else
				bundleArray = new JSONArray(goodsInfo.getGoodsDetial());
			List<FoodInfo> foodInfoList = new ArrayList<FoodInfo>();
			for (int i = 0; i < bundleArray.length(); i++)
			{
				FoodInfo foodInfo = new FoodInfo(bundleArray.optJSONObject(i));
				foodInfoList.add(foodInfo);
			}
			GoodsInfo info = ShoppingCartManager.addPackageDetailToGoods(goodsInfo, foodInfoList, isInsertDb);
			JSONObject jsonObject = ShoppingCartManager.getGoodsDataJson(info);
			jsonArray.put(jsonObject);
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return jsonArray;
	}

	/**
	 * 获取活动添加到购物车之前的数据
	 * @return
	 */
	public static JSONArray getActiveDataBeforeAddToShop(GoodsInfo goodsInfo, boolean isInsertDb)
	{
		JSONArray jsonArray = null;
		try
		{
			jsonArray = new JSONArray();
			List<ActiveInfo> activeInfoList = new ArrayList<ActiveInfo>();
			JSONArray activeArray = null;
			if (TextUtils.isEmpty(goodsInfo.getGoodsDetial()))
				activeArray = new JSONArray();
			else
				activeArray = new JSONArray(goodsInfo.getGoodsDetial());
			for (int i = 0; i < activeArray.length(); i++)
			{
				ActiveInfo activeInfo = new ActiveInfo(activeArray.optJSONObject(i));
				activeInfoList.add(activeInfo);
			}
			GoodsInfo info = ShoppingCartManager.addActiveDetailToGoods(goodsInfo, activeInfoList, isInsertDb);
			JSONObject jsonObject = ShoppingCartManager.getGoodsDataJson(info);
			jsonArray.put(jsonObject);
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return jsonArray;
	}

	/**
	 * 单品提交订单时获取单品
	 *
	 * @return
	 */
	public static JSONArray getGoodsArray(GoodsInfo goodInfo)
	{
		JSONArray goodsArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();
		try
		{
			if (goodInfo.getGoods_type() == VodConstant.RETAIL_TYPE)
			{
				jsonObject.put("food_id", goodInfo.getGoodsId());
				jsonObject.put("type", "goods");
				jsonObject.put("quantity", goodInfo.getOrderNum());
				jsonObject.put("is_present", (goodInfo.is_give() || goodInfo.is_present()) ? 1 : 0);
				jsonObject.put("order_time", TimeUtils.getNowTime());
				String goodsFlavor = goodInfo.getFlavor();
				String goodsStandard = goodInfo.getStandard();
				JSONArray tastes = new JSONArray();
				JSONArray standard = new JSONArray();
				if (!TextUtils.isEmpty(goodsFlavor))
				{
					JSONArray array = new JSONArray(goodsFlavor);
					if (array.length() > 0)
					{
						for (int i = 0; i < array.length(); i++)
						{
							JSONObject obj = array.getJSONObject(i);
							tastes.put(obj.optString("name"));
						}
					}
					jsonObject.put("tastes", tastes);
				}
				if (!TextUtils.isEmpty(goodsStandard))
				{
					JSONArray array = new JSONArray(goodsStandard);
					if (array.length() > 0)
					{
						for (int i = 0; i < array.length(); i++)
						{
							JSONObject obj = array.optJSONObject(i);
							standard.put(obj.optString("name"));
						}
					}
					jsonObject.put("standards", standard);
				}
				jsonObject.put("goods", new JSONArray());
				goodsArray.put(jsonObject);
			}
		} catch (JSONException e)
		{
			e.printStackTrace();
			return null;
		}
		return goodsArray;
	}

	/**
	 * 微信确认点餐确定下单时获取点餐数据
	 *
	 * @param weChatInfoList
	 * @return
	 */
	public static JSONArray getWeChatOrderGoodsArray(List<WeChatInfo> weChatInfoList)
	{
		JSONArray jsonArray = new JSONArray();
		if (CollectionUtils.isEmpty(weChatInfoList))
			return jsonArray;
		else
		{
			try
			{
				for (int i = 0; i < weChatInfoList.size(); i++)
				{
					WeChatInfo weChatInfo = weChatInfoList.get(i);
					List<GoodsInfo> goodsList = weChatInfo.getGoodsInfoList();
					if (!CollectionUtils.isEmpty(goodsList))
					{
						JSONObject jsonObject = new JSONObject();
						jsonObject.put("order_id", weChatInfo.getOrderId());
						JSONArray goodsArray = new JSONArray();
						for (int j = 0; j < goodsList.size(); j++)
						{
							JSONObject object = new JSONObject();
							GoodsInfo goodsInfo = goodsList.get(j);
							object.put("id", goodsInfo.getGoodsId());
							object.put("quantity", goodsInfo.getOrderNum());
							object.put("tastes", TextUtils.isEmpty(goodsInfo.getFlavor()) ? new JSONArray() : new JSONArray(goodsInfo.getFlavor()));
							goodsArray.put(object);
						}
						jsonObject.put("foods", goodsArray);
						jsonArray.put(jsonObject);
					}
				}
			} catch (JSONException e)
			{
				e.printStackTrace();
			}
			return jsonArray;
		}
	}

	/**
	 * 获取购物车下单时商品或者活动的数据
	 * @param goodsInfo 商品信息
	 * @return 下单数据
	 */
	public static JSONObject getGoodsDataJson(GoodsInfo goodsInfo)
	{
		JSONObject jsonObject = null;
		try
		{
			jsonObject = new JSONObject();
			jsonObject.put("food_id", goodsInfo.getGoodsId());
			if (goodsInfo.getGoods_type() == VodConstant.RETAIL_TYPE || goodsInfo.getGoods_type() == VodConstant.BUNDLE_TYPE)
				jsonObject.put("type", "goods");
			else
				jsonObject.put("type", "active");
			jsonObject.put("quantity", goodsInfo.getOrderNum());
			jsonObject.put("is_present", (goodsInfo.is_give() || goodsInfo.is_present()) ? 1 : 0);
			jsonObject.put("order_time", TextUtils.isEmpty(goodsInfo.getOrderTime()) ? TimeUtils.getNowTime() : goodsInfo.getOrderTime());
			String goodsFlavor = goodsInfo.getFlavor();
			String goodsStandard = goodsInfo.getStandard();
			JSONArray tastes = new JSONArray();
			JSONArray standard = new JSONArray();
			if (!TextUtils.isEmpty(goodsFlavor))
			{
				JSONArray array = new JSONArray(goodsFlavor);
				if (array.length() > 0)
				{
					for (int i = 0; i < array.length(); i++)
					{
						JSONObject obj = array.getJSONObject(i);
						tastes.put(obj.optString("name"));
					}
				}
				jsonObject.put("tastes", tastes);
			}
			if (!TextUtils.isEmpty(goodsStandard))
			{
				JSONArray array = new JSONArray(goodsStandard);
				if (array.length() > 0)
				{
					for (int i = 0; i < array.length(); i++)
					{
						JSONObject obj = array.getJSONObject(i);
						standard.put(obj.optString("name"));
					}
				}
				jsonObject.put("standards", standard);
			}
			//商品详情，套餐或者活动
			String goodsDetail = goodsInfo.getGoodsDetial();

			if (!TextUtils.isEmpty(goodsDetail))
			{
				JSONArray detailArray = new JSONArray();
				JSONArray jArray = new JSONArray(goodsDetail);
				if (jArray.length() > 0)
				{
					for (int j = 0; j < jArray.length(); j++)
					{
						JSONObject obj = jArray.getJSONObject(j);
						String combos = obj.optString("combos");
						if (TextUtils.isEmpty(combos))
						{
							//活动里面的单品 或者套餐里面的单品
							getDetailData(detailArray, obj, goodsInfo.getGoods_type());//活动里面的单品不加加combosId，否则加
						} else
						{
							//活动里面的套餐
							JSONArray array = new JSONArray(combos);
							JSONArray goodJsonArray = new JSONArray();
							if (array.length() > 0)
							{
								JSONObject object = new JSONObject();
								object.put("goods_id", obj.optString("goods_id"));
								object.put("quantity", obj.optInt("number"));
								object.put("tastes", new JSONArray());
								object.put("standards", new JSONArray());
								for (int i = 0; i < array.length(); i++)
								{
									JSONObject object1 = array.getJSONObject(i);
									getDetailData(goodJsonArray, object1, 0);
								}
								object.put("goods", goodJsonArray);
								detailArray.put(object);
							}
						}
					}
				}
				jsonObject.put("goods", detailArray);
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return jsonObject;
	}

	/**
	 * 组装详情数据
	 * @param detailArray
	 * @param obj
	 */
	private static void getDetailData(JSONArray detailArray, JSONObject obj, int type)
	{
		try
		{
			String flavor_array = obj.optString("flavor_array");
			if (!TextUtils.isEmpty(flavor_array))
			{
				//添加有口味的单品
				JSONArray jsonArray = new JSONArray(flavor_array);
				if (jsonArray.length() > 0)
				{
					for (int k = 0; k < jsonArray.length(); k++)
					{
						if (jsonArray.optJSONObject(k).optInt("number") > 0)
						{
							JSONObject object = new JSONObject();
							object.put("goods_id", obj.optString("goods_id"));
							object.put("quantity", jsonArray.getJSONObject(k).optInt("number"));
							String flavor = jsonArray.getJSONObject(k).optString("flavor");
							JSONArray arrayFlavor = new JSONArray(flavor);
							JSONArray goodsTastes = new JSONArray();
							if (arrayFlavor.length() > 0)
							{
								for (int i = 0; i < arrayFlavor.length(); i++)
								{
									JSONObject o = arrayFlavor.getJSONObject(i);
									goodsTastes.put(o.optString("name"));
								}
							}
							object.put("tastes", goodsTastes);
							object.put("standards", new JSONArray());
							if (type == VodConstant.ACTIVE_TYPE)
								object.put("goods", new JSONArray());
							else
								object.put("combo_id", obj.optString("combo_id"));
							detailArray.put(object);
						}
					}
				}
			} else
			{
				if (obj.optInt("number") > 0)
				{
					JSONObject object = new JSONObject();
					object.put("goods_id", obj.optString("goods_id"));
					object.put("quantity", obj.optInt("number"));
					String standard = obj.optString("standard");
					if (!TextUtils.isEmpty(standard))
					{
						JSONArray array = new JSONArray(standard);
						JSONArray standardArray = new JSONArray();
						if (array.length() > 0)
						{
							for (int i = 0; i < array.length(); i++)
							{
								JSONObject stand = array.optJSONObject(i);
								standardArray.put(stand.optString("name"));
							}
						}
						object.put("standards", standardArray);
					}
					object.put("tastes", new JSONArray());
					if (type == VodConstant.ACTIVE_TYPE)
						object.put("goods", new JSONArray());
					else
						object.put("combo_id", obj.optString("combo_id"));
					detailArray.put(object);
				}
			}
		} catch (JSONException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 是否配置有默认规格
	 * @param value
	 * @return
	 */
	public static boolean isSelectStandard(String value)
	{
		if (TextUtils.isEmpty(value))
			return false;
		boolean isTrue = false;
		try
		{
			JSONArray jsonArray = new JSONArray(value);
			for (int i = 0; i < jsonArray.length(); i++)
			{
				JSONObject object = jsonArray.optJSONObject(i);
				JSONArray defaultArray = object.optJSONArray("default_standard_ids");//默认选中的规格ids
				if (defaultArray != null && defaultArray.length() > 0)
				{
					isTrue = true;
				} else
				{
					isTrue = false;
				}
			}
		} catch (Exception e)
		{
			e.printStackTrace();
			return isTrue;
		}
		return isTrue;
	}

	/**
	 * 从规格获取默认选择的规格 套餐详情或者活动详情特有
	 * @param value 规格
	 */
	public static String getStandardDetail(String value)
	{
		if (TextUtils.isEmpty(value))
			return "";
		JSONArray standard = new JSONArray();
		try
		{
			JSONArray jsonArray = new JSONArray(value);
			for (int i = 0; i < jsonArray.length(); i++)
			{
				JSONObject object = jsonArray.optJSONObject(i);
				JSONArray defaultArray = object.optJSONArray("default_standard_ids");//默认选中的规格ids
				JSONArray array = object.optJSONArray("standard");
				if (defaultArray != null && array != null)
					for (int j = 0; j < defaultArray.length(); j++)
					{
						String standard_id = defaultArray.optString(j);
						for (int k = 0; k < array.length(); k++)
						{
							JSONObject jsonObject = array.optJSONObject(k);
							if (standard_id.equals(jsonObject.optString("standard_id")))
							{
								JSONObject obj = new JSONObject();
								obj.put("id", jsonObject.optString("standard_id"));
								obj.put("name", jsonObject.optString("name"));
								obj.put("standardTypeId", jsonObject.optString("standard_type_id"));
								standard.put(obj);
								break;
							}
						}
					}
			}
		} catch (Exception e)
		{
			e.printStackTrace();
			return "";
		}
		return standard.length() > 0 ? standard.toString() : "";
	}
}
